package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.MovieBean;
import com.project.bean.OrderBean;
import com.project.bean.PageBean;
import com.project.bean.UserBean;
import com.project.service.IMoveService;
import com.project.service.IOrderService;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("sale")
public class SaleController {
    @Autowired
    private IUserService service;
    @Autowired
    private IMoveService moveService;
    @Autowired
    private IOrderService orderService;

    @RequestMapping("login")
    public UserBean login(String userName, String pwd, HttpServletRequest req) {
        UserBean user = service.login(userName, pwd);
        req.getSession().setAttribute("loginUser", user);
        return user;
    }

    //查看所有可购买的电影票
    @RequestMapping("cutByItem")
    public PageBean<MovieBean> cutByItem(Integer pageNO, String name, String storeName, String matches, String rowNum) {
        return moveService.cutByItem(pageNO, name, storeName, matches, rowNum);
    }

    //下单
    @RequestMapping("buyMovie")
    public String buyMovie(Integer movieId,HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        MovieBean movie = moveService.findById(movieId);
        orderService.addOrder(new OrderBean(user.getUserName(),movie.getName(),movie.getMatches(),movie.getStoreName(),movie.getRowNum(),movie.getSeatNum()));
        moveService.buyMovie(movie);
        return "ok";
    }

    //查看自己的所有订单信息
    @RequestMapping("findAllByUserId")
    public List<OrderBean> findAllByUserId(HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        System.out.println(user);
        return orderService.findByUser(user.getUserName());
    }

}
